﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="Runtime.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the Runtime type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------

using System;
using System.Data;
using System.Xml.Linq;

namespace Ornament.NHibernates.Configurations
{
    /// <summary>
    /// </summary>
    public sealed class Runtime : PropertyContainer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Runtime"/> class. 
        /// </summary>
        /// <param name="sessionFactoryElement">
        /// The session factory element.
        /// </param>
        public Runtime(XElement sessionFactoryElement) : base(sessionFactoryElement)
        {
        }

        /// <summary>
        /// Command Timeout
        /// </summary>
        public int Timeout
        {
            get
            {
                string s = GetPropertyValue("command_timeout");
                return s == null ? 30 : Convert.ToInt32(s);
            }

            set { SetPropertyValue("command_timeout", value.ToString()); }
        }

        /// <summary>
        /// Gets or sets ShowSql.
        /// </summary>
        /// <value>
        /// The show sql.
        /// </value>
        public bool ShowSql
        {
            get
            {
                string vlaue = GetPropertyValue("show_sql");
                return vlaue != null && Convert.ToBoolean(vlaue);
            }

            set { SetPropertyValue("show_sql", value.ToString().ToLower()); }
        }

        /// <summary>
        /// gets or sets user outer jon for sql
        /// </summary>
        public bool UseOuterJoin
        {
            get
            {
                string value = GetPropertyValue("use_outer_join");
                return value != null && Convert.ToBoolean(value);
            }

            set { SetPropertyValue("use_outer_join", value.ToString().ToLower()); }
        }

        /// <summary>
        /// gets or sets 数据库隔离级别
        /// </summary>
        public IsolationLevel Isolation
        {
            get
            {
                string value = GetPropertyValue("connection.isolation");
                if (value == null)
                {
                    return IsolationLevel.ReadCommitted;
                }

                return (IsolationLevel) Enum.Parse(typeof (IsolationLevel), value);
            }

            set { SetPropertyValue("connection.isolation", value.ToString()); }
        }

        /// <summary>
        /// Gets or sets BatchSize.
        /// </summary>
        /// <value>
        /// The batch size.
        /// </value>
        public int BatchSize
        {
            get
            {
                string value = GetPropertyValue("adonet.batch_size");
                return value == null ? 5 : Convert.ToInt32(value);
            }

            set { SetPropertyValue("adonet.batch_size", value.ToString()); }
        }
    }
}